#include "CardDataParser.h"

cardDataParser::cardDataParser()
{
}

void cardDataParser::ReadACard(ifstream& infile)
{
	string currentLine;				//The current line the parser is reading
	CardData curCard;				//The current card being read

	getline(infile, currentLine);			//Parse the name (String)
	currentLine = currentLine.substr(6);
	curCard.setName(currentLine);

	getline(infile, currentLine);			//Parse the points (Int)
	currentLine = currentLine.substr(8);
	curCard.setPoints(atoi(currentLine.c_str()));

	getline(infile, currentLine);			//Parse the color (Int bitflag)
	currentLine = currentLine.substr(7);
	curCard.setColor(ParseCardColors(currentLine));

	getline(infile, currentLine);			//Parse the features (Int bitflag)
	currentLine = currentLine.substr(10);
	curCard.setFeatures(ParseCardFeatures(currentLine));

	getline(infile, currentLine);			//Parse the power (human readable string)
	curCard.setPower(currentLine);

	getline(infile, currentLine);			//Parse the ID number (int)
	currentLine = currentLine.substr(4);
	curCard.setID(atoi(currentLine.c_str()));

	getline(infile, currentLine);			//Parse the value (int)
	currentLine = currentLine.substr(6);
	curCard.setValue(atoi(currentLine.c_str()));

	getline(infile, currentLine);			//Parse the influence (int bit-flag)
	currentLine = currentLine.substr(11);
	curCard.setInfluence(ParseInfluence(currentLine));

	getline(infile, currentLine);			//Parse the target attributes (int bit-flag)
	currentLine = currentLine.substr(18);
	curCard.setTargetAttributes(ParseCardFeatures(currentLine));

	getline(infile, currentLine);			//Parse target color (int bit-flag)
	currentLine = currentLine.substr(13);
	curCard.setTargetColor(ParseCardColors(currentLine));

	getline(infile, currentLine);			//Parse target ID (int)
	currentLine = currentLine.substr(11);
	curCard.setTargetID(atoi(currentLine.c_str()));

	getline(infile, currentLine);			//Parse target location
	currentLine = currentLine.substr(17);
	curCard.setTargetArea(ParseTargetLocation(currentLine));

	this->cardRead.push(curCard);
}

int cardDataParser::ParseCardColors(string colorString)
{
	int aColor = 0;							//Start with a "colorless" card (Null value)

	if (colorString == "Purple")
	{
		aColor = TAR_PURPLE;
	}
	else if (colorString == "Black")
	{
		aColor = TAR_BLACK;
	}
	else if (colorString == "Green")
	{
		aColor = TAR_GREEN;
	}
	else if (colorString == "Orange")
	{
		aColor = TAR_ORANGE;
	}
	else if (colorString == "Blue")
	{
		aColor = TAR_BLUE;
	}
	else if (colorString == "Yellow")
	{
		aColor = TAR_YELLOW;
	}
	else if (colorString == "Silver")
	{
		aColor = TAR_SILVER;
	}
	else if (colorString == "Red")
	{
		aColor = TAR_RED;
	}
	else
	{
		aColor = 0;
	}

	return aColor;
}

int cardDataParser::ParseInfluence(string influenceString)
{
	int anInfluence = 0;		//An empty influence flag
	string lookingAt = "";		//The substring that we will be looking at and changing repeatedly.
	int lookPosition = 0;		//The position in the influenceString we left of at in the last iteration
	int prevLookPosition = 0;	//The previous value of lookPosition

	lookPosition = influenceString.find(',');	//Priming Read
	while (lookPosition != -1)
	{
		lookingAt = influenceString.substr(prevLookPosition, lookPosition - prevLookPosition); //Look only at the first word you find.

		if (lookingAt == "Self")
		{
			anInfluence += SELF_INFLUENCE;
		}
		else if (lookingAt == "Target")
		{
			anInfluence += TARGET_INFLUENCE;
		}
		else if (lookingAt == "Opposite")
		{
			anInfluence += OPPOSITE_INFLUENCE;
		}
		else if (lookingAt == "Adjacent")
		{
			anInfluence += ADJACENT_INFLUENCE;
		}
		else
		{
			anInfluence += NONE;
		}
		prevLookPosition = lookPosition + 1;
		lookPosition = influenceString.find(',' , prevLookPosition);
	}
	return anInfluence;
}

int cardDataParser::ParseTargetLocation(string targetAreaString)
{
	int aTargetArea = 0;		//An empty influence flag
	string lookingAt = "";		//The substring that we will be looking at and changing repeatedly.
	int lookPosition = 0;		//The position in the targetAreaString we left of at in the last iteration
	int prevLookPosition = 0;	//The previous value of lookPosition

	lookPosition = targetAreaString.find(',');	//Priming Read
	while (lookPosition != -1)
	{
		lookingAt = targetAreaString.substr(prevLookPosition, lookPosition - prevLookPosition); //Look only at the first word you find.

		if (lookingAt == "Self")
		{
			aTargetArea = SELF_TARGET;
		}
		else if (lookingAt == "Opposite")
		{
			aTargetArea = OPPOSITE_TARGET;
		}
		else if (lookingAt == "Adjacent")
		{
			aTargetArea = ADJACENT_TARGET;
		}
		else if (lookingAt == "Inplay")
		{
			aTargetArea = INPLAY_TARGET;
		}
		else
		{
			aTargetArea = NONE;
		}
		prevLookPosition = lookPosition + 1;
		lookPosition = targetAreaString.find(',' , prevLookPosition);
	}
	return aTargetArea;
}

int cardDataParser::ParseCardFeatures(string featureString)
{
	int aSetOfFeatures = 0;		//The int that represents our feature flags
	string lookingAt = "";		//The substring that we will be looking at and changing repeatedly.
	int lookPosition = 0;		//The position in the feature string we left of at in the last iteration.
	int prevLookPosition = 0;	//The previous value of lookPosition
	
	lookPosition = featureString.find(',');	//Priming Read
	while (lookPosition != -1)	//While we haven't reached the end of the feature list.
	{
		lookingAt = featureString.substr(prevLookPosition, lookPosition - prevLookPosition);

		if (lookingAt == "Male")
		{
			aSetOfFeatures += MALE;
		}
		else if (lookingAt == "Female")
		{
			aSetOfFeatures += FEMALE;
		}
		else if (lookingAt == "Hero")
		{
			aSetOfFeatures += HERO;
		}
		else if (lookingAt == "Prop")
		{
			aSetOfFeatures += PROP;
		}
		else if (lookingAt == "Villain")
		{
			aSetOfFeatures += VILLAIN;
		}
		else if (lookingAt == "Animal")
		{
			aSetOfFeatures += ANIMAL;
		}
		else if (lookingAt == "Place")
		{
			aSetOfFeatures += PLACE;
		}
		else
		{
			aSetOfFeatures += NONE;
		}
		prevLookPosition = lookPosition + 1;
		lookPosition = featureString.find(',' , prevLookPosition);
	}

	return aSetOfFeatures;
}

int cardDataParser::ReadDataFromFile(ifstream& infile)
{
	if (infile.is_open())
	{
		while (!infile.eof())
		{
			this->ReadACard(infile);
			infile.ignore(256, '\n');
		}
	}
	else
	{
		cout << "The file couldn't be opened. \n";
		return 1;
	}
	infile.close();
	return 0;
}
int cardDataParser::TestingOutput()
{
	for (int i = cardRead.size(); i > 0; i--)
	{
		cout << cardRead.front().getName() << endl;
		cout << cardRead.front().getPoints() << endl;
		cout << cardRead.front().getColor() << endl;
		cout << cardRead.front().getFeatures() << endl;
		cout << cardRead.front().getPower() << endl;
		cout << cardRead.front().getID() << endl;
		cout << cardRead.front().getValue() << endl;
		cout << cardRead.front().getInfluence() << endl;
		cout << cardRead.front().getTargetAttributes() << endl;
		cout << cardRead.front().getTargetColor() << endl;
		cout << cardRead.front().getTargetID() << endl;
		cout << cardRead.front().getTargetArea() << endl;
		cout << endl;

		cardRead.pop();
	}
	return 0;
}

void cardDataParser::getData(CardData* cards[])
{
	int i = 0;
	while(!cardRead.empty() && i < 12)
	{
		cards[i] = new CardData(cardRead.front());
		cardRead.pop();
		++i;
	}
}